using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float hurtSpeed;
    public Vector2 hurtDirecttion;
    public bool isHurt;


    private AnimatorStateInfo info;
    private Animator animator;
    private Animator hitAnimator;
    new private Rigidbody2D rb;


    private void Start() {
        animator = transform.GetComponent<Animator>();
        rb = transform.GetComponent<Rigidbody2D>();
        hitAnimator = transform.GetChild(0).GetComponent<Animator>();
    }

    private void Update() {
        info = animator.GetCurrentAnimatorStateInfo(0);
        if(isHurt){
            rb.velocity = hurtDirecttion * hurtSpeed;
            if(info.normalizedTime > .6f)
                isHurt = false;
        }
    }

    public void GetHurt(Vector2 direction){
        transform.localScale = new Vector3(-direction.x,1,1);
        isHurt = true;
        hurtDirecttion = direction;
        animator.SetTrigger("Hurt");
        hitAnimator.SetTrigger("Hit");
    }
}
